//
//  HZMTableDetailsType1VC.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/10/28.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMTableDetailsType1VC.h"

@interface HZMTableDetailsType1VC ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain) UICollectionView *collection;
@property(nonatomic, retain) HZMCHeader *data;
@property(nonatomic, retain) NSMutableArray *productArr;
@property(nonatomic, retain) NSMutableArray *picArr;

@end

@implementation HZMTableDetailsType1VC

- (void)dealloc
{
    [_collection release];
    [_strUrl release];
    [_data release];
    [_productArr release];
    [_picArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    
    [self navigationSet];
    
    [self createCollectionView];
    
    [self createData];
}



#pragma mark CollectionView Setting
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake((WIDTH - 10) / 2, 2 * HEIGHT / 5);
    flowLayout.headerReferenceSize = CGSizeMake(WIDTH, 15 * HEIGHT / 31);
    
    if (!self.navigationController.hidesBarsOnSwipe) {
        
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:flowLayout];
    } else {
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    }
    
    [self.view addSubview:self.collection];
    [_collection release];
    self.collection.backgroundColor = [UIColor whiteColor];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    [self.collection registerClass:[HZMType1CVCell class] forCellWithReuseIdentifier:@"collectionReuse"];
    
    [self.collection registerClass:[HZMCellDetailsCRV class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"c_header_reuse"];
}

#pragma mark CollectionView Delegate Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.productArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.picArr = [HZMPic baseModelByArr:(NSMutableArray *)[self.productArr[indexPath.row] pic]];
    HZMType1CVCell *cell = [self.collection dequeueReusableCellWithReuseIdentifier:@"collectionReuse" forIndexPath:indexPath];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[self.picArr[0] pic]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
    cell.priceLabel.text = [NSString stringWithFormat:@"羊%@", [self.productArr[indexPath.row] price]];
    cell.likesLabel.text = [self.data likes];
    cell.titleLabel.text = [self.productArr[indexPath.row] desc];
    cell.likesLabel.text = [self.productArr[indexPath.row] likes];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"单品详情购买页面");
}

#pragma mark CollectionReusableView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind ==UICollectionElementKindSectionHeader) {
        HZMCellDetailsCRV *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"c_header_reuse" forIndexPath:indexPath];
        reuseableView.backgroundColor = [UIColor redColor];
        [reuseableView.topImageView sd_setImageWithURL:[NSURL URLWithString:self.data.pic]];
        reuseableView.imageViewTitle.text = self.data.title;
        reuseableView.desc.text = self.data.desc;
        return reuseableView;
    }
    return nil;
}



#pragma mark Navigation Setting
- (void)navigationSet
{
    self.navigationItem.title = @"专题详情";
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(-20, 0, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"nav_back@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
    
    self.navigationController.hidesBarsOnSwipe = YES;
}

#pragma mark Navigationbar ButtonAction
- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark Data
- (void)createData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:self.strUrl parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        self.data = [[HZMCHeader alloc] init];
        [self.data setValuesForKeysWithDictionary:dic[@"data"]];
        [self.data release];
        
        self.productArr = [NSMutableArray array];
        self.productArr = [HZMItemDetails baseModelByArr:dic[@"data"][@"product"]];
        
        self.picArr = [NSMutableArray array];
        
        [self.collection reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
